<?php
namespace Modules\Activity\Http\Controllers\Client\Seckill;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Seckill\Activity;


use Validator;
use ThrowException;
use Auth;
use DB;

// 参与活动的用户（is_lucker_user）
class ActivityController extends Controller
{

     // 列表
    public function index(Request $req)
    {
        $options = $req->all();
        // $options['per_page'] = 200;
        $options['scopes'] = ['onStock'];
        $options['only'] = [
            'id',
            'uuid',
            'money_price',
            'score_price',
            'discount_score_price',
            'discount_money_price',
            'title',
            'thumb',
            'activity_type_text',
            'activity_type',
            'union_status',
            'union_status_text',
            'start_at',
            'end_at',
            'stock'
            // 'skus'  => [
            //     '*' => [
            //         'id',
            //         'uuid',
            //         'thumb',
            //         'title',
            //         'number',
            //         'level'
            //     ]
            // ]
        ];

        $list = Activity::getList($options);

        return $list->generateListResponse();
    }


    // 详情
    public function show(Request $req, $uuid)
    {
        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $info = $item->getInfo();

        // 阅读量+1
        $item->update([
            'visit_total' => $item->visit_total + 1
        ]);

        return [
            'info' => $info
        ];
    }

    public function checkUserGroup(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $userGroup = $item->user_group;

        $info = $item->getInfo([
            'id',
            'uuid',
            'title',
            'discount_money_price' => 'money_price',
            'discount_score_price' => 'score_price'
        ]);

        // $group->

        list($isPassed, $options) = $userGroup->checkRule($user);

        return [
            'info' => $info,
            'is_passed' => $isPassed,
            'options' => $options
        ];
    }
}
